package com.canal.admin.controller;

import com.canal.admin.exception.BuzException;
import com.canal.admin.response.Result;
import com.canal.admin.response.ResultUtil;
import com.canal.admin.service.QiniuService;
import com.canal.admin.service.ZcolChannelItemService;
import com.canal.admin.transfer.req.item.ChannelItemAddReq;
import com.canal.admin.transfer.req.item.DiscountSetReq;
import com.canal.admin.transfer.req.item.ItemListConditions;
import com.canal.admin.transfer.req.item.ItemUpReq;
import com.canal.admin.transfer.resp.PageResp;
import com.canal.admin.transfer.resp.item.ChannelItemListResp;
import com.canal.admin.transfer.resp.item.ItemDetailResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author yangbo
 * @date 2020-06-24 8:39
 */
@RestController
public class ChannelItemController extends BaseController {

    private static final String APK_TYPE = "apk";

    @Autowired
    private ZcolChannelItemService channelItemService;

    @Autowired
    private QiniuService qiniuService;

    /**
     * 列表搜索
     * @param conditions
     * @param bindingResult
     * @return
     * @throws BuzException
     */
    @PostMapping(value = "/channel/item/list")
    @PreAuthorize("isAuthenticated()")
    public Result<PageResp<ChannelItemListResp>> list(@RequestBody @Validated ItemListConditions conditions,
                                                      BindingResult bindingResult) throws BuzException {
        dealReqInvalidError(bindingResult);
        return ResultUtil.makeSuccess(channelItemService.list(conditions));
    }

    /**
     * 新增渠道号
     * @param addReq
     * @param bindingResult
     * @return
     * @throws BuzException
     */
    @PostMapping(value = "/channel/item/add")
    @PreAuthorize("isAuthenticated()")
    public Result add(@RequestBody @Validated ChannelItemAddReq addReq,
                      BindingResult bindingResult) throws BuzException {
        dealReqInvalidError(bindingResult);
        channelItemService.addItem(addReq);
        return ResultUtil.makeSuccess("新增成功");
    }

    /**
     * 设置折扣
     * @param setReq
     * @param bindingResult
     * @return
     */
    @PostMapping(value = "/channel/item/discount/set")
    @PreAuthorize("isAuthenticated()")
    public Result setDiscountRate(@RequestBody @Validated DiscountSetReq setReq,
                                  BindingResult bindingResult) throws BuzException {
        dealReqInvalidError(bindingResult);
        channelItemService.setDiscount(setReq);
        return ResultUtil.makeSuccess("设置成功");
    }

    /**
     * 设置进量状态
     * @param id
     * @return
     * @throws BuzException
     */
    @PostMapping(value = "/channel/item/state/set/{id}")
    @PreAuthorize("isAuthenticated()")
    public Result setInputState(@PathVariable Integer id) throws BuzException {
        channelItemService.setInputState(id);
        return ResultUtil.makeSuccess("设置成功");
    }

    /**
     * 批量删除
     * @param willDelIds
     * @return
     */
    @PostMapping(value = "/channel/item/del")
    @PreAuthorize("isAuthenticated()")
    public Result<List<Integer>> batchDel(@RequestBody List<Integer> willDelIds) throws BuzException {
        //执行删除操作
        return ResultUtil.makeSuccess("删除成功",channelItemService.batchDel(willDelIds));
    }

    /**
     * 查询明细
     * @param id
     * @return
     */
    @GetMapping(value = "/channel/item/detail/{id}")
    @PreAuthorize("isAuthenticated()")
    public Result<ItemDetailResp> detail(@PathVariable Integer id) {
        return ResultUtil.makeSuccess(channelItemService.detail(id));
    }

    /**
     * 编辑
     * @param upReq
     * @param bindingResult
     * @return
     * @throws BuzException
     */
    @PostMapping(value = "/channel/item/update")
    @PreAuthorize("isAuthenticated()")
    public Result update(@RequestBody @Validated ItemUpReq upReq,
                         BindingResult bindingResult) throws BuzException {
        dealReqInvalidError(bindingResult);
        channelItemService.update(upReq);
        return ResultUtil.makeSuccess("编辑成功");
    }

    /**
     * apk文件上传
     * @param file
     * @return
     * @throws BuzException
     * @throws IOException
     */
    @PostMapping(value = "/channel/item/apk/upload")
    public Result<String> uploadApk(@RequestPart(name = "file") MultipartFile file) throws BuzException, IOException {
        String name = file.getOriginalFilename();

        if(!name.contains(APK_TYPE)) {
            throw new BuzException(400,"上传文件类型错误");
        }
        name = name.substring(0,name.length() - 4);
        String url = qiniuService.upload(name,".apk",file.getInputStream());
        return ResultUtil.makeSuccess("success",url);
    }

    /**
     * 渠道查询列表
     * @param keyword
     * @return
     */
    @GetMapping(value = "/channel/query/list")
    @PreAuthorize("isAuthenticated()")
    public Result<List<Map<String,Object>>> channelList(@RequestParam(value = "keyword") String keyword) {
        return ResultUtil.makeSuccess(channelItemService.channelList(keyword));
    }
}
